<?php
namespace Views;
/**
 * File holds class for generating feedback view
 *
 * PHP version 5.3.5
 *
 * @category   CM
 * @package    Views
 * @subpackage -
 * @author     markus karileet <markuskarileet@hotmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @link       -
 */
class Feedback extends Page {
	private $_service;
	
	public function __construct($language, $onlyContent = 0) {
		$this->setUp($language, $onlyContent);
		$this->_service = new \Services\Feedback();
	}
	
	/**
	 * Method assigns businessId and businessName template variables according
	 * to business code
	 * @param var $business Business code
	 */
	private function _getBusinessDetailsForFeedback($business) {
		//map for beautiful titles
		$businessService = new \Services\Business();
		$b = $businessService->getBusinessByCode($business);
		//business not in db, need to update!
		if (!$b) {
			$b = $businessService->insertTemplate($business);
		}
		
		$this->_smarty->assign('business', $b);
	}
	
	/**
	 * Method shows feedback form to user. Title is generated from attribute
	 * If no match is found, attribute will be used as name!
	 * @param var $business Business identifier code
	 * @return void
	 */
	public function viewForm($business) {
		$this->_getBusinessDetailsForFeedback($business);
		$this->_content = $this->_smarty->fetch('feedbackForm.tpl');
		$this->display();
	}
	
	/**
	 * Method stores feedback to database
	 * @param array $post HTML POST array
	 */
	public function saveForm($post) {
		if ($this->_service->prepareSave($post) > 0) {
			$message = array('success' => $this->_translation['feedbackSent']);
		} else {
			$message = array('error' => $this->_translation['feedbackNotSent']);
		}
		if ($post['mobile'] == 0) {
			$topView = new \Views\Business($_SESSION['l']);
			$topView->setMessage($message);
			$topView->viewBusinessDetails($post['business']);
		} else {
			echo('<h1>Thank You!</h1>');
		}
	}
	
	/**
	 * Method shows mobile view for feedback
	 * @param var $business Business code
	 */
	public function viewPortableForm($business) {
		$this->_getBusinessDetailsForFeedback($business);
		$this->_smarty->display('portableFeedback.tpl');
	}

}